package cn.jkgly.wxUser.controller;

import cn.jkgly.wxUser.entity.Result;
import cn.jkgly.wxUser.entity.WxPcBind;
import cn.jkgly.wxUser.service.IWxPcBindService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class WxPcBindController {
    @Autowired
    IWxPcBindService wxPcBindService;

    /**
     * 根据openid或pcId查询绑定用户
     * @param openid
     * @param pcId
     * @param bindType
     * @return
     */
    @GetMapping("/auth/getWxPcUser")
    public Result getOneWxPcUser(@RequestParam(name = "openid") String openid,@RequestParam(name = "pcId") String pcId,@RequestParam(name="bindType")String bindType){
        QueryWrapper<WxPcBind> queryWrapper =new QueryWrapper<>();
        if(openid!=null||"".equals(openid)){
            queryWrapper.eq("openid",openid);
        }else{
            queryWrapper.eq("pc_id",pcId);
        }
        queryWrapper.eq("bind_type",bindType);

        WxPcBind wxPcBind=wxPcBindService.getOne(queryWrapper);
        if (wxPcBind==null){
            return Result.error("用户未找到");
        }
        return Result.ok(wxPcBind);
    }

    /**
     * 微信电脑用户绑定接口
     * @param wxPcBind
     * @return
     */
    @PostMapping("/auth/addWxPcUser")
    public Result addWxPcUser(@RequestBody WxPcBind wxPcBind){
        wxPcBind.setCreateTime(new Date());
        boolean success = wxPcBindService.save(wxPcBind);
        if (!success){
            return Result.error("绑定失败");
        }
        return Result.ok("绑定成功");
    }
    /**
     * 根据pcId查询绑定用户列表
     * @param pcId
     * @param bindType
     * @return
     */
    @GetMapping("/auth/getListWxPcUser/{pcId}/{bindType}")
    public Result getListWxPcUser(@PathVariable("pcId")  String pcId,@PathVariable("bindType") String bindType){

        List<WxPcBind> wxPcBind=wxPcBindService.getListWxPc(pcId,bindType);
        if (wxPcBind==null||wxPcBind.size()==0){
            return Result.error("用户未找到");
        }
        return Result.ok(wxPcBind);
    } /**
     * 根据openid删除绑定pc用户
     * @param openid
     * @param bindType
     * @return
     */
    @DeleteMapping("/auth/deleteWxPcUser/{openid}/{bindType}")
    public Result deleteWxPcUser(@PathVariable("openid")  String openid,@PathVariable("bindType") String bindType){
        Map map = new HashMap();
        map.put("openid",openid);
        map.put("bind_type",bindType);
        boolean delete =wxPcBindService.removeByMap(map);
        if (delete){
            return Result.ok("删除成功");
        }
        return Result.error("删除失败");
    }
}
